import toInt from 'licia/toInt'
import fileExportApi from '@/api/fileexport/fileExport'
import { Loading, Message } from 'element-ui'
import fileExportStatusEnum from '@/enums/fileExportStatusEnum'

export function exportFile(api, data) {
  const loading = Loading.service({
    lock: true,
    text: '导出中，请稍后',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  })
  api(data).then(response => {
    if (response.data.code === 200) {
      var fileExportId = response.data.data
      var interv = setInterval(function() {
        var postData = {}
        postData.id = fileExportId
        postData.skipLoading = true
        fileExportApi.query(postData).then(response => {
          if (response.data.code === 200) {
            if (toInt(response.data.data.exportStatus) === fileExportStatusEnum.SUCCESS.value) {
              loading.close()
              clearInterval(interv)
              location.href = response.data.data.downloadUrl
              return
            }
            if (toInt(response.data.data.exportStatus) === fileExportStatusEnum.FAIL.value) {
              loading.close()
              clearInterval(interv)
              Message.error('导出失败')
              return
            }
          }
        })
      }, 1000)
    } else {
      Message.error(response.data.msg)
      loading.close()
    }
  })
}
